﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ProductInventoryController : Controller
    {
        //
        // GET: /ProductInventory/

        private readonly IGodownentryRepository _godownentryRepository;
        private readonly IDeliveryorderRepository _deliveryorderRepository;
        private readonly IProductRepository _productRepository;
        private readonly IInventoryRepository _inventoryRepository;
        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IProductDetailInRepository _productDetailInRepository;
        public ProductInventoryController(IProductDetailInRepository productDetailInRepository,IDeliveryorderRepository deliveryorderRepository, IGodownentryRepository godownentryRepository, IWarehouseRepository warehouseRepository, IProductRepository productRepository, IInventoryRepository inventoryRepository)
        {
            _deliveryorderRepository = deliveryorderRepository;
            _productRepository = productRepository;
            _inventoryRepository = inventoryRepository;
            _warehouseRepository = warehouseRepository;
            _godownentryRepository = godownentryRepository;
            _productDetailInRepository = productDetailInRepository;
        }
        private IEnumerable<ProductInventoryModel> GetProductInventoryModels()
        {
            return from p in _productRepository.GetAll().AsQueryable()
                   from i in _inventoryRepository.GetAll().AsQueryable()
                   where p.ID==i.Product.ID
                   select new ProductInventoryModel
                              {
                                  ProductId = p.ID,
                                  Name = p.Name,
                                  ProductCategoy = p.ProductCategory.Name,
                                  InventroyCount = i.Count,
                              };
        }
        #region 
        private IEnumerable<ProductInDetailModel> GetProductInDetailModels()
        {
            return from p in _productDetailInRepository.GetAll().AsQueryable()
                   from g in _godownentryRepository.GetAll().AsQueryable()
                   where p.Product.ID == g.Product.ID
                   select new ProductInDetailModel
                              {
                                  ID =p.ID,
                                  DetailNumberIn = p.DetailNumberIn,
                                  InPrice = p.InPrict,
                                  Count =p.Count1,
                                  Totail = p.Total,
                                  ProductName = p.Product.Name,
                                  Intime = g.Datetime,
                                  BinnName = g.Billproducer,
                                  EmployeeName = g.employee.Name,
                                  ProductProviderName = g.ProductProvider.Name,
                              };
        }
        #endregion
        [GridAction]
        public ActionResult GetProductIndetail(int productID)
        {
            var productdetailin = _productDetailInRepository.GetMany(x => x.Product.ID == productID).ToList();
            var godownentry = _godownentryRepository.GetMany(x => x.Product.ID == productID).ToList();
            var productin = from p in productdetailin
                            from g in godownentry
                            where p.ID==g.ProductDetailIn.ID
                            select new ProductInDetailModel
                                       {
                                           ID = p.ID,
                                           DetailNumberIn = p.DetailNumberIn,
                                           InPrice = p.InPrict,
                                           Count = p.Count1,
                                           Totail = p.Total,
                                           ProductName = p.Product.Name,
                                           Intime = g.Datetime,
                                           BinnName = g.Billproducer,
                                           EmployeeName = g.employee.Name,
                                           ProductProviderName = g.ProductProvider.Name,
                                       };
            return View(new GridModel(productin));
        }

        [GridAction]
        public ActionResult Index()
        {
            return View(new GridModel(GetProductInventoryModels()));
        }

    }
}
